package com.fly.lambda.controller;

import com.fly.lambda.domain.Message;
import com.fly.lambda.framework.dao.Pagination;
import com.fly.lambda.framework.utils.CommonUtils;
import com.fly.lambda.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Fly on 2016/7/29.
 */
@Controller
public class MessageCtrl {

    @Autowired
    MessageService messageService;

    @RequestMapping("/submitMessage")
    @ResponseBody
    public Map<String, String> submitMessage(
            @RequestParam(value = "firstName", required = false, defaultValue = "")String firstName,
            @RequestParam(value = "lastName", required = false, defaultValue = "")String lastName,
            @RequestParam(value = "email", required = false, defaultValue = "")String email,
            @RequestParam(value = "phoneNumber", required = false, defaultValue = "")String phoneNumber,
            @RequestParam(value = "content", required = false, defaultValue = "")String content
    ){
        Map<String, String> map = new HashMap<String, String>();
        Message message = new Message();
        message.setMessageId(CommonUtils.genPK());
        message.setName(lastName + " " + firstName);
        message.setEmail(email);
        message.setPhoneNumber(phoneNumber);
        message.setMassageContent(content);
        message.setCreateTime(CommonUtils.dateToStr(new Date(), "yyyy-MM-dd HH:mm:ss"));
        messageService.createMessage(message);
        map.put("result", "success");
        return map;
    }

    @RequestMapping("/manage/message/list")
    public String toMessageList(
            ModelMap model
    ){
        return "/manage/message/list";
    }

    @RequestMapping("/manage/message/getMessageData")
    public String getMessageData(
            @RequestParam(value = "read", required = false, defaultValue = "")String read,
            @RequestParam(value = "curPage", required = false, defaultValue = "1")int curPage,
            @RequestParam(value = "pageSize", required = false, defaultValue = "15")int pageSize,
            ModelMap model
    ){
        Pagination<Message> messagePage = messageService.queryMessagePage(read, curPage, pageSize);
        model.addAttribute("messagePage", messagePage);
        return "/manage/message/messageData";
    }

    @RequestMapping("/manage/message/delete")
    @ResponseBody
    public Map<String, String> deleteMessage(
            @RequestParam("messageId")String messageId
    ){
        Map<String, String> map = new HashMap<String, String>();
        messageService.deleteMessage(messageId);
        map.put("result", "success");
        return map;
    }

    @RequestMapping("/manage/message/read")
    @ResponseBody
    public Map<String, String> readMessage(
            @RequestParam("messageId")String messageId
    ){
        Map<String, String> map = new HashMap<String, String>();
        messageService.readMessage(messageId);
        map.put("result", "success");
        return map;
    }
}
